package cn.com.dhc.chesishanbigdemo;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.dhc.chesishanbigdemo.User;

/**
 * Web Layer(Controller Layer / Action Layer) Service
 * Layer((service)/serviceimpl) Dao Layer
 */
public class RegisterLoginAction extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private RegisterService service;

	/**
	 * Constructor of the object.
	 */
	public RegisterLoginAction() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = request.getContextPath();
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		// PrintWriter out = response.getWriter();

		String username = request.getParameter("username");
		String pswd = request.getParameter("pswd");
		System.out.println("username = " + username + " pswd = " + pswd);
		User user = new User(username, pswd);

		// 为名字和密码创建 Cookies
		Cookie Username = new Cookie("username",request.getParameter("username"));
		Cookie Pswd = new Cookie("pswd", request.getParameter("pswd"));

		// 为两个 Cookies 设置过期日期为 24 小时后
		Username.setMaxAge(60 * 60 * 24);
		Pswd.setMaxAge(60 * 60 * 24);

		// 在响应头中添加两个 Cookies
		response.addCookie(Username);
		response.addCookie(Pswd);

		/*
		 * String sex = request.getParameter("sex"); String age =
		 * request.getParameter("age"); String phone =
		 * request.getParameter("phone"); String mail =
		 * request.getParameter("mail");
		 */
		// valid

		/*
		 * System.out.println("sex=" + sex + " age = " + age + " phone " + phone
		 * + " mail " + mail); Information information = new Information(sex,
		 * age, phone, mail);
		 */

		boolean registerUserSuccess = service.registerUser(user);
		/*
		 * boolean registerInfoSuccess =
		 * service.registerInformatin(information);
		 */
		/* if (registerUserSuccess && registerInfoSuccess) */
		if (registerUserSuccess) {
			request.setAttribute(username, pswd);
			// server dispatch
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/information.jsp");
			dispatcher.forward(request, response);
		} else {
			// client redirect.
			response.sendRedirect(path + "/login.jsp");
		}
		// 概念 -> 实现 -> Spring -> SpringMVC -> 实现好了 -> 项目 -> 原理
		// SpringMVC -> 函数互相跳 -> AJAX -> 找一个框架. -> Filter -> sysout
		// -> Spring -> SpringMVC -> 视频 马士兵 -> 100多集 ->
		// 前一天晚上 看视频 -> 会用 -> 原理
		// Sping Servlet 无关/ SpringMVC Servlet 有关
		// MyBatis
		// Servlet -> 类里面互相跳
		// form -> get / post 
		// ajax -> get / post -- result -> js > result
		// code format
		// AJAX -> JSON -> 拼一下 / (net.sf.json / google.fastjson / alibaba.fastjson)
		// 什么时候开始Spring -> 带着你做项目 -> 
		// prototype -> 3 个配置文件 -> MyBatis集成Spring.
		// Spring集成SpringMVC Web集成SpringMVC.
		
		// fastjson
		// mybatis 3.2.x
		// spring 4.x.x
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		service = new RegisterServiceImpl();
	}

}